using System;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using System.Collections.Generic;
using System.IO;


public class Browser2 : ListView
{
    public DirectoryInfo pwd { get; set; }
    public ListViewItem up;
    public List<FileSystemInfo> entries;
    public ComboBox comboPwd;
    public Archive parItFile;
    public BrowserState2 state;

    public Browser2()
    {
        state = new DirectoryState2(this);
        up = new ListViewItem();
        up.Text = "..";
        up.ImageIndex = 0;
    }

    protected override void InitLayout()
    {
        base.InitLayout();
        pwd = new DirectoryInfo(Directory.GetCurrentDirectory());
        state.refresh();
    }

    public void activate()
    {
        state.activate();
    }

    public void chdir(string path)
    {
        pwd = new DirectoryInfo(path);
        refresh();
    }
    public void refresh()
    {
        state.refresh();
    }

    public List<FileSystemInfo> getSelectedFiles()
    {
        return new List<FileSystemInfo>();
    }
}

public abstract class BrowserState2
{
    public BrowserState2(Browser2 context)
    {
        this.context = context;
    }

    public abstract void refresh();
    public abstract void activate();
    protected Browser2 context;
}

public class ParItState2 : BrowserState2
{
    public ParItState2(Browser2 context) : base(context)
    {
        refresh();
    }

    public override void refresh()
    {
        context.Items.Clear();
        context.Items.Add(context.up);
    }

    public override void activate()
    {
        ListViewItem eersteSelectie = context.SelectedItems[0];

        if (eersteSelectie.Index == 0)
        {
            context.state = new DirectoryState2(context);
            context.refresh();
        }
    }
}

public class DirectoryState2 : BrowserState2
{
    public DirectoryState2(Browser2 context) : base(context)
    {
        
    }

    public override void refresh()
    {
        context.Items.Clear();
        context.Items.Add(context.up);
        try
        {
            context.entries = context.pwd.EnumerateFileSystemInfos().ToList();
        }
        catch (NullReferenceException)
        {
        }

        foreach (FileSystemInfo entry in context.entries)
        {
            ListViewItem item = new ListViewItem();

            if (entry.GetType().Equals(typeof(DirectoryInfo)))
                item.ImageIndex = 0;
            else if (entry.GetType().Equals(typeof(FileInfo)) && entry.Name.EndsWith(".prt"))
                item.ImageIndex = 2;
            else if (entry.GetType().Equals(typeof(FileInfo)))
                item.ImageIndex = 1;

            item.Text = entry.Name;
            context.Items.Add(item);
        }
    }

    public override void activate()
    {
        ListViewItem eersteSelectie = context.SelectedItems[0];

        if (eersteSelectie.Index == 0)
        {
            // kan anders
            try
            {
                context.pwd = context.pwd.Parent;
            }
            catch
            {
                Console.WriteLine("Er bevind zich geen directory boven deze.");
            }
            context.refresh();
        }
        else
        {
            int index = eersteSelectie.Index - 1;
            FileSystemInfo entry = context.entries[index];
            Type fileOrFolder = entry.GetType();

            if (fileOrFolder.Equals(typeof(DirectoryInfo)))
            {
                context.chdir(entry.FullName);
            }
            else if (fileOrFolder.Equals(typeof(FileInfo)) && entry.Name.EndsWith(".prt"))
            {
                context.parItFile = new Archive();
                context.parItFile.fileName = entry.FullName;
                context.state = new ParItState2(context);
            }
        }
    }
}
